﻿#include "stdafx.h"
#include "ProgramData.h"
#include <locale>
#include "WHRS.h"

const BYTE g_BeginShakeHand[7] = { 0x50, 0x52, 0x4F, 0x47, 0x52, 0x41, 0x4D };

DWORD	g_LanId = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);	//当前语言
std::set<CString> g_sPortSet;

CString FormatID(DWORD id)
{
	CString str;
	str.Format(theApp.m_iDMRType == DPMR ? _T("%07d") : _T("%08d"), id);
	return str;
}


//CString bt3ToStr(const BYTE * bf)
//{	
//	DWORD dw = static_cast<DWORD>(bf[2]);//bf小尾字节序，bf[2]高位字节
//	dw = (dw << 8) | bf[1];
//	dw = (dw << 8) | bf[0];
//	return FormatID(dw);
//}

std::string UnicodeToUtf8(const CString& unicode)
{
	int len = WideCharToMultiByte(CP_UTF8, 0, unicode, unicode.GetLength(), NULL, 0, NULL, NULL);
	std::string str;
	str.resize(len);
	WideCharToMultiByte(CP_UTF8, 0, unicode, unicode.GetLength(), const_cast<char*>(str.data()), len, NULL, NULL);
	return str;
}

CString UTF8ToUnicode(const std::string& sU8)
{
	//int l = sU8.length();
	//预转换，得到所需空间的大小;
	int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, sU8.c_str(), sU8.length(), NULL, 0);

	//分配空间要给'\0'留个空间，MultiByteToWideChar不会给'\0'空间
	//wchar_t* wszString = new wchar_t[wcsLen + 1];
	CString unicodeString;
	LPTSTR pstr = unicodeString.GetBufferSetLength(wcsLen);
	//转换
	::MultiByteToWideChar(CP_UTF8, NULL, sU8.c_str(), sU8.length(), pstr, wcsLen);

	//最后加上'\0'
	//wszString[wcsLen] = '\0';
	//delete[] wszString;
	//wszString = NULL;

	return unicodeString;
}


//CString bt3ToStr(const BYTE * bf);
//CString AnsiToUnicode(LPCSTR str, int len)
//{
//	static TCHAR buf[MAX_PATH];
//	ZeroMemory(buf, MAX_PATH);
//	if (len == 0)
//		len = ::MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
//	ASSERT(len < MAX_PATH);
//	::MultiByteToWideChar(CP_ACP, 0, str, len, buf, len);
//	return CString(buf);
//}
//inline CString AnsiToUnicode(const std::string& str){
//	return AnsiToUnicode(str.c_str(), str.length());
//}
std::string AnsiToUTF8(LPCSTR AnsiStr, int len){
	if (len == 0) return std::string();
	//AnsiToUnicode(AnsiStr, len);	
	int ret = ::MultiByteToWideChar(CP_ACP, 0, AnsiStr, len, NULL, 0);
	CString unicode;
	wchar_t *buf = unicode.GetBuffer(ret);
	::MultiByteToWideChar(CP_ACP, 0, AnsiStr, len, buf, ret);
	unicode.ReleaseBuffer();
	return UnicodeToUtf8(unicode);
}


WORD GetCRC(const BYTE* pBuf, int bufLen)
{
	WORD tmp, crc = 0;
	int i, count = 0;
	while (count < bufLen)
	{
		tmp = pBuf[count];
		tmp = tmp << 8;
		crc ^= tmp;
		for (i = 0; i < 8; i++)
		{
			if (crc & 0x8000){//32768==0x8000
				crc = crc << 1;
				crc ^= 0x1021;//4129==0x1021;
			}
			else{
				crc = crc << 1;
			}
		}
		count++;
	}
	return crc;
}

//////////////////*****************************信道*****************************///////////////////


//是否设置了强调
//bool TChannel::IsPreemphasis()
//{
//	return (_State3 & 0x01) > 0;
//}

//void TChannel::SetTxFreq(CString sFreq)
//{
//	StrToBt4(sFreq, _TxFreq);
//}
//CString TChannel::GetTxFreq()
//{
//	return Bt4ToStr(_TxFreq);
//}
//void TChannel::SetRxFreq(CString sFreq)
//{
//	StrToBt4(sFreq, _RxFreq);
//}
//CString TChannel::GetRxFreq()
//{
//	return Bt4ToStr(_RxFreq);
//}

//////////////////*****************************区域*****************************///////////////////

void TZone::SetChannelScanAdd(int iChan, bool bEnable)
{
	ASSERT(iChan < 16 && iChan >= 0);
	if (iChan >= 16 || iChan < 0) return;
	WORD wScan = _ScanAdd[1];
	wScan = (wScan << 8) | _ScanAdd[0];
	WORD wTmp = 0x0001 << iChan;
	if (bEnable)
		wScan |= wTmp;
	else
		wScan &= ~wTmp;
	_ScanAdd[1] = HIBYTE(wScan);
	_ScanAdd[0] = LOBYTE(wScan);
}
bool TZone::IsChannelScanAdd(int iChan)
{
	ASSERT(iChan < 16 && iChan >= 0);
	if (iChan >= 16 || iChan < 0) return false;
	WORD wScan = _ScanAdd[1];
	wScan = (wScan << 8) | _ScanAdd[0];
	WORD wTmp = 0x0001 << iChan;
	return (wScan & wTmp) != 0;
}
bool TZone::AddChannelAt(int iIndex, int iChanCount)
{
	ASSERT(iChanCount < MAX_CHANNEL && iIndex < MAX_CHANNEL);
	if (!(iChanCount < MAX_CHANNEL && iIndex < MAX_CHANNEL)){
		return false;
	}
	//iChanCount为当前区域的总信道数
	if (iIndex >= iChanCount){//没有覆盖掉原有信道数据
		_Channel[iIndex].SetDefaultLinkMan(0);
		_Channel[iIndex].SetGroupID(0);
		_Channel[iIndex].SetIndex(iIndex+1);//索引从1开始
		return true;
	}
	memmove(&_Channel[iIndex + 1], &_Channel[iIndex], (iChanCount - iIndex - 1) * sizeof(TChannel));
	for (int i = iIndex; i < iChanCount + 1; ++i)
	{//_Channel[i].SetIndex(++i) ==等价于==> { ++i; _Channel[i].SetIndex(i); }
		_Channel[i].SetIndex(i + 1);//索引从1开始
	}
	return true;
}

bool TZone::RemoveChannelAt(int iIndex)
{
	ASSERT(iIndex < MAX_CHANNEL&&iIndex>=0);
	if (!(iIndex < MAX_CHANNEL &&iIndex>=0)) return false;
	::CopyMemory(_Channel + iIndex, _Channel + iIndex + 1, (MAX_CHANNEL - iIndex - 1) * sizeof(TChannel));
	for (int i = iIndex; i < MAX_CHANNEL; ++i)
	{//_Channel[i].SetIndex(++i) ==等价于==> { ++i; _Channel[i].SetIndex(i); }
		_Channel[i].SetIndex(i + 1);//索引从1开始
	}
	return true;
}


//////////////////*****************************联系人*****************************///////////////////
TLM TLinkMan::GetLM(){
	TLM lm;
	lm._Index = _Index;
	lm._LMID[0] = _LMID[0];
	lm._LMID[1] = _LMID[1];
	lm._LMID[2] = _LMID[2];
	return lm;
}
//CString TLinkMan::GetNumber()
//{
//	return bt3ToStr(_LMID);
//}
//void TLinkMan::SetNumber(CString sNumber)
//{
//	strToBt3(sNumber, _LMID);
//}
//CString TLinkMan::GetName()
//{
//	return AnsiToUnicode((LPSTR)_LMName, 10);
//}
//void TLinkMan::SetName(CString sName)
//{
//	UnicodeToAnsi(sName, (LPSTR)_LMName, 10);
//	//int l = ::WideCharToMultiByte(CP_ACP, 0, Unistr, -1, nullptr, 0, NULL, NULL);
//	/*BOOL b = FALSE;
//	::WideCharToMultiByte(CP_ACP, 0, sName, sName.GetLength(), (LPSTR)_LMName, 10, "\0", &b);*/
//}
//bool TLinkMan::IsLinkManEqual(BYTE* bt)
//{
//	return bt[0] == _LMID[0] && bt[1] == _LMID[1] && bt[2] == _LMID[2];
//}

//////////////////*****************************短信*****************************///////////////////

//void CSMS::SetSMS(const BYTE* buf, DWORD buflen)
//{
//	memcpy_s(m_SMS, 40, buf, buflen);
//}
//void CSMS::GetSMS(BYTE* buf)
//{
//	memcpy_s(buf, 40, m_SMS, 40);
//}

//void CSMS::SetSMS(CString strSMS)
//{
//#ifdef UNICODE
//	//int l = ::WideCharToMultiByte(CP_ACP, 0, strSMS.GetString(), -1, nullptr, 0, NULL, NULL);
//	::WideCharToMultiByte(CP_ACP, 0, strSMS.GetString(), -1, (LPSTR)m_SMS, 40, NULL, NULL);
//#else
//	memcpy_s(m_SMS, 40, strSMS.GetString(), strSMS.GetLength());
//#endif // UNICODE
//}
//
//CString CSMS::GetSMS()
//{
//#ifdef UNICODE
//	static TCHAR buf[40];
//	//int l = ::MultiByteToWideChar(CP_ACP, 0, (LPSTR)m_SMS, 40, NULL, 0);
//	::MultiByteToWideChar(CP_ACP, 0, (LPSTR)m_SMS, 40, buf, 40);
//	return CString(buf);
//#else
//	return CString(m_SMS);
//#endif // UNICODE
//}
//////////////////*****************************呼叫组*****************************///////////////////


//int TGroup::AddLinkMan(TLM& lm)
//{	
//	if (_ItemCount >= MAX_GROUPMEMBER){
//		ASSERT(FALSE);
//		return -1;
//	}	
//	_LinkMan[_ItemCount]._Index = lm._Index;
//	_LinkMan[_ItemCount]._LMID[0] = lm._LMID[0];
//	_LinkMan[_ItemCount]._LMID[1] = lm._LMID[1];
//	_LinkMan[_ItemCount]._LMID[2] = lm._LMID[2];
//	return _ItemCount++;
//}
//bool TGroup::RemoveLinkMan(int iLM)
//{
//	if (iLM >= _ItemCount || iLM < 0) {
//		ASSERT(FALSE);
//		return false;
//	}
//	if (iLM < --_ItemCount){
//		CopyMemory(_LinkMan + iLM, _LinkMan + iLM + 1, (_ItemCount - iLM) * sizeof(TLM));
//	}
//	return true;
//}
//////////////////****************************扫描列表******************************///////////////////

int TScanList::AddChannel(TChanMember member)
{
	if (_ItemCount >= MAX_CHANNEL){
		ASSERT(FALSE);
		return -1;
	}
	ASSERT(member._ZoneIndex >= 1 && member._ZoneIndex <= MAX_ZONE);
	ASSERT(member._ChannelIndex >= 1 && member._ChannelIndex <= MAX_CHANNEL);
	_ChanList[_ItemCount] = member;
	return _ItemCount++;
}
int TScanList::AddChannel(BYTE bZone1Base, BYTE bChan1Base)
{
	ASSERT(bZone1Base >= 1 && bZone1Base <= MAX_ZONE);
	ASSERT(bChan1Base >= 1 && bChan1Base <= MAX_CHANNEL);
	if (_ItemCount >= MAX_CHANNEL){
		return -1;
	}
	_ChanList[_ItemCount]._ZoneIndex = bZone1Base;
	_ChanList[_ItemCount]._ChannelIndex = bChan1Base;
	return _ItemCount++;
}
bool TScanList::RemoveChannel(int iIndex)
{
	if (iIndex >= _ItemCount || iIndex < 0){
		ASSERT(FALSE);
		return false;
	}
	if (iIndex < --_ItemCount){
		CopyMemory(_ChanList + iIndex, _ChanList + iIndex + 1, (_ItemCount - iIndex) * sizeof(TChanMember));
	}
	return true;
}
bool TScanList::RemoveChannel(BYTE bZone1Base, BYTE bChan1Base)
{
	bool b = false;
	for (int i = 0; i < _ItemCount; ){
		if (_ChanList[_ItemCount]._ZoneIndex == bZone1Base 
			&& _ChanList[_ItemCount]._ChannelIndex == bChan1Base){
			b |= RemoveChannel(i);
			continue;
		}
		++i;
	}
	return b;
}
bool TScanList::RemoveZone(BYTE bZone1Base)
{
	bool b = false;
	for (int i = 0; i < _ItemCount;){
		if (_ChanList[_ItemCount]._ZoneIndex == bZone1Base){
			b |= RemoveChannel(i);
			continue;
		}
		++i;
	}
	return b;
}

//////////////////**********************************************************///////////////////
